{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "61c84b15-ec12-46f5-81c8-2c03708e4a02",
   "metadata": {},
   "source": [
    "# Svm\n",
    "调用API实现Ensumble Learning，预测哪些乘客被异常运送"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f9e942e3-3209-45c2-a449-c658f405ad78",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import VotingClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from xgboost import XGBClassifier\n",
    "from joblib import dump, load\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b76499a3-51bb-4e91-8642-396ca53d267f",
   "metadata": {},
   "source": [
    "### 加载数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "727669cc-982c-48df-b903-2c147e66054f",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = pd.read_csv('../../FeatureSelectedData/selected_train.csv')\n",
    "X_train = train_data.iloc[:, :-1]\n",
    "y_train = train_data.iloc[:, -1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "189610fc-dff5-4aaf-9472-9e836c0080a5",
   "metadata": {},
   "source": [
    "### 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62e6275a-918a-45cb-a5d9-c0206b0fdb19",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义多个基学习器\n",
    "clf1 = LogisticRegression(max_iter=200, random_state=42)\n",
    "clf2 = RandomForestClassifier(n_estimators=100, random_state=42)\n",
    "clf3 = XGBClassifier(n_estimators=100, random_state=42)\n",
    "\n",
    "# Voting (可选 soft/hard)\n",
    "voting_clf = VotingClassifier(\n",
    "    estimators=[\n",
    "        (\"lr\", clf1),\n",
    "        (\"rf\", clf2),\n",
    "        (\"xgb\", clf3)\n",
    "    ],\n",
    "    voting='soft'  # soft 更利用概率, 一般表现更好\n",
    ")\n",
    "\n",
    "# 训练\n",
    "voting_clf.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76123fba-5f26-46da-9a4b-4a2c7b45108f",
   "metadata": {},
   "outputs": [],
   "source": [
    "dump(voting_clf, '../../ModelFile/EL/el_model.joblib')\n",
    "print(\"Model saved as el_model.joblib\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bdd5cc3-9735-4f11-99ac-67d1c04dafc5",
   "metadata": {},
   "source": [
    "### 将模型封装为 `EL()` 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d98a92f-0edf-4a58-abec-eb604a9b1a97",
   "metadata": {},
   "outputs": [],
   "source": [
    " def EL(X):\n",
    "    clf = load('../../ModelFile/EL/el_model.joblib')\n",
    "    y_predicted = clf.predict(X)\n",
    "    return y_predicted"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "037d6543-81dd-4e27-912f-62cf1fdeec99",
   "metadata": {},
   "source": [
    "### 利用 `EL()` 函数进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0f89657-1aa5-40ed-a2f9-22c74f7a4b39",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_test_data = pd.read_csv('../../FeatureSelectedData/selected_test.csv')\n",
    "result_file = pd.read_csv('../../RawData/sample_submission.csv')\n",
    "predictions = EL(final_test_data)\n",
    "predictions_bool = (predictions == 1)\n",
    "result_file[\"Transported\"] = pd.Series(predictions_bool, result_file.index)\n",
    "result_file.to_csv('../../predictions/EL/result_el.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "360403f3-4836-439b-90f5-d6c2a6a55f3b",
   "metadata": {},
   "source": [
    "### 将数据集放到 `kaggle` 上评分"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
